#ifndef __GNASH_MANAGER_H__
#define __GNASH_MANAGER_H__

#include "utils/GnashUtils.h"
#include "librender/Renderer_agg.h"

using namespace Ogre;
using namespace gnash;

class GnashManager 
	{
	public:
		GnashManager();
		~GnashManager();

		static GnashManager* getSingletonPtr( void );

		void init();
		void loadGui(string guiXml);
		void createOgreAssets(int width, int height);

		movie_root* root;
		static movie_root* getRoot() {return getSingletonPtr()->root;}

		boost::shared_ptr<Renderer_agg_base> mRendererAggBase;

		Texture* texture;
		Overlay* overlay;
		OverlayContainer* panel;

		boost::scoped_array<unsigned char> offscreenbuf;
		int offscreenbuf_size;

		std::auto_ptr<RunResources> runResources;
		SystemClock systemClock;
		InterruptableVirtualClock* vclock;

		//inline void blit() { GnashUtils::blit(offscreenbuf.get(), texture); }
//    static void blit() { GnashUtils::blit(GnashManager::getSingletonPtr()->offscreenbuf.get(), GnashManager::getSingletonPtr()->texture); }
		static void update() {
			GnashManager* instance = GnashManager::getSingletonPtr();
//			assert(instance->mRenderer);
			instance->vclock->resume();
//			instance->root->advanceMovie();
			instance->root->advance();
			instance->root->display();
		}
		
		
		static void blit() {
			GnashManager* instance = GnashManager::getSingletonPtr();
			unsigned char* bmp = instance->offscreenbuf.get();
			Texture* tex = instance->texture;
//			assert(tex!=NULL);
//			GnashUtils::clearTexture(tex);
	
			HardwarePixelBuffer* buf = tex->getBuffer().getPointer();
			buf->blitFromMemory(PixelBox(tex->getWidth(), tex->getHeight(), tex->getDepth(),PF_B8G8R8A8, bmp));
//			buf->blitFromMemory(PixelBox(tex->getWidth(), tex->getHeight(), tex->getDepth(), PF_BYTE_BGRA, bmp));
		}

	protected:
		static GnashManager *mGnashManager;

	};

#endif
